package com.ffcs.cn.action.scene;

import com.supermap.data.Workspace;
import com.supermap.desktop.controls.ui.UICommonToolkit;
import com.supermap.desktop.core.Application;
import com.supermap.desktop.core.Interface.IBaseItem;
import com.supermap.desktop.core.Interface.IFormScene;
import com.supermap.desktop.core.implement.CtrlAction;
import com.ffcs.cn.DevelopProperties;
import com.supermap.ui.SceneControl;

import javax.swing.*;
import java.text.MessageFormat;

/**
 * 保存场景
 *
  */
public class CtrlActionSaveScene extends CtrlAction {
	@Override
	protected void run() {
		if (!(Application.getActiveApplication().getActiveForm() instanceof IFormScene)) {
			return;
		}
		if (UICommonToolkit.showConfirmDialogYesNo(MessageFormat.format(DevelopProperties.getString("String_SaveScenePrompt"), Application.getActiveApplication().getActiveForm().getTitle())) == JOptionPane.OK_OPTION) {
			IFormScene activeForm = (IFormScene) Application.getActiveApplication().getActiveForm();
			SceneControl sceneControl = activeForm.getSceneControl();
			try {
				if (activeForm.isNeedSave()) {
					Workspace workspace = sceneControl.getScene().getWorkspace();
					boolean succeed;
					// 若是当前场景存在，直接原地保存
					if (workspace.getScenes().indexOf(activeForm.getTitle()) >= 0) {
						succeed = workspace.getScenes().setSceneXML(activeForm.getTitle(), sceneControl.getScene().toXML());
					} else {
						// 若是当前场景不存在，创建一个新场景
						succeed = workspace.getScenes().add(activeForm.getTitle(), sceneControl.getScene().toXML()) >= 0;
					}
					if (succeed) {
						Application.getActiveApplication().getOutput().output(DevelopProperties.getString("String_SaveScene_Succeed"));
					} else {
						Application.getActiveApplication().getOutput().output(DevelopProperties.getString("String_SaveScene_Failed"));
					}
				}
			} catch (Exception ex) {
				Application.getActiveApplication().getOutput().output(ex);
			}
		}
	}

	@Override
	public boolean enable() {
		return Application.getActiveApplication().getActiveForm() instanceof IFormScene;
	}

	public CtrlActionSaveScene(IBaseItem caller) {
		super(caller);
	}
}
